﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Informaiton_Retrieval_Hw2.com.Model;

namespace Informaiton_Retrieval_Hw2
{
    public partial class MainForm : Form
    {
        Model model;
        public MainForm()
        {
            InitializeComponent();
            MainForm.CheckForIllegalCrossThreadCalls = false;
            model = new Model();
            model.updateUIEvent += this.update;
            model.updateCorrectQueryEvent += this.updateCorrect;
            update();
        }

        void update() {
            this.progressBar1.Value = model.Percent;
            outPutTextBox.Text = model.outPut;

            openButton.Enabled = !model.isLoading&&!model.isSearching;
            submitButton.Enabled = !model.isLoading && !model.isSearching&&model.isLoaded;
            simpleSearchButton.Enabled = !model.isLoading && !model.isSearching && model.isLoaded;
            queryBox.Enabled = !model.isLoading && !model.isSearching && model.isLoaded;
           
        }

        void updateCorrect() 
        {
            queryBox.Text = model.correctQueryText;
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog1.FileName;
                model.Load(filePath);
            }
        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            string queryText = queryBox.Text;
            if (queryText != null && queryText.Length > 0)
                model.Search(queryText);
        }

        private void simpleSearchButton_Click(object sender, EventArgs e)
        {
            string queryText = queryBox.Text;
            if (queryText != null && queryText.Length > 0)
                model.simpleSearch(queryText);
        }
    }
}
